package com.cmnit.utils;

import org.apache.log4j.Logger;

import java.io.InputStream;
import java.util.Properties;

/**
 * 配置文件工具类
 *
 * @author chan
 * @version 1.0
 * @since 2022.3.3
 */
public class ConfigurationManager {
    // 创建配置对象
    private static Properties prop = new Properties();
    private static final Logger log = Logger.getLogger(ConfigurationManager.class);

    // 获取配置文件
    static {
        String properties = "product.properties";
        setProperties(properties);
    }

    // 获取配置文件
    public static void setProperties(String properties) {
        try {
            //通过流读取配置文件
            InputStream inputStream = ConfigurationManager.class.getClassLoader().getResourceAsStream(properties);
            //加载配置文件
            prop.load(inputStream);
            log.info("load properties：" + properties);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // 获取配置项
    public static String getProperty(String key) {
        return prop.getProperty(key);
    }

    // 获取数值参数
    public static Integer getIntProperty(String key) {
        return Integer.parseInt(getProperty(key));
    }

    // 获取配置项
    public static String getProperty(String key, String defaultValue) {
        return prop.getProperty(key, defaultValue);
    }

    // 获取布尔类型的配置项
    public static boolean getBoolean(String key) {
        String value = prop.getProperty(key);
        try {
            return Boolean.parseBoolean(value);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
}
